package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
	private static final String USER = "root";
	private static final String PASSWORD = "1106601121";
	public static final String URL = "jdbc:mysql://localhost/studentsystem?characterEncoding=utf8&characterSetResults=utf8";
	private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static Connection conn = null;
	static {
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	public static    Connection getConnection() throws SQLException {
		if (conn == null) {
			try {
				conn = DriverManager.getConnection(URL, USER, PASSWORD);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return conn;

		}
		return conn;
	}

	public static void closeAll(ResultSet rs, Connection conn, PreparedStatement pstmt) {
		// TODO Auto-generated method stub
		if (rs!=null){				
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} 		
		if (pstmt!=null){				
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} 
		if (conn!=null){				
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	}

